{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# 共享单车数量预测案例——线性回归分析\n",
    "\n",
    "在这个案例中，我们将利用美国一家公司提供的共享单车信息数据训练和测试一个模型，并对模型的性能和预测能力进行测试。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、导入必要的工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 矩阵操作\n",
    "import pandas as pd # SQL数据处理\n",
    "\n",
    "from sklearn.metrics import r2_score  #评价回归预测模型的性能\n",
    "\n",
    "import matplotlib.pyplot as plt   #画图\n",
    "import seaborn as sns\n",
    "\n",
    "# 图形出现在Notebook里而不是新窗口\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 读取数据\n",
    "做完特征工程后的数据，请先运行FE_BikeShareing.ipynb，得到文件day.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.355170</td>\n",
       "      <td>0.373517</td>\n",
       "      <td>0.828620</td>\n",
       "      <td>0.284606</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.379232</td>\n",
       "      <td>0.360541</td>\n",
       "      <td>0.715771</td>\n",
       "      <td>0.466215</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.171000</td>\n",
       "      <td>0.144830</td>\n",
       "      <td>0.449638</td>\n",
       "      <td>0.465740</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.175530</td>\n",
       "      <td>0.174649</td>\n",
       "      <td>0.607131</td>\n",
       "      <td>0.284297</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.209120</td>\n",
       "      <td>0.197158</td>\n",
       "      <td>0.449313</td>\n",
       "      <td>0.339143</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant  season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  \\\n",
       "0        1         1         0         0         0       1       0       0   \n",
       "1        2         1         0         0         0       1       0       0   \n",
       "2        3         1         0         0         0       1       0       0   \n",
       "3        4         1         0         0         0       1       0       0   \n",
       "4        5         1         0         0         0       1       0       0   \n",
       "\n",
       "   mnth_4  mnth_5  ...   weekday_5  weekday_6      temp     atemp       hum  \\\n",
       "0       0       0  ...           0          1  0.355170  0.373517  0.828620   \n",
       "1       0       0  ...           0          0  0.379232  0.360541  0.715771   \n",
       "2       0       0  ...           0          0  0.171000  0.144830  0.449638   \n",
       "3       0       0  ...           0          0  0.175530  0.174649  0.607131   \n",
       "4       0       0  ...           0          0  0.209120  0.197158  0.449313   \n",
       "\n",
       "   windspeed  holiday  workingday  yr   cnt  \n",
       "0   0.284606        0           0   0   985  \n",
       "1   0.466215        0           0   0   801  \n",
       "2   0.465740        0           1   0  1349  \n",
       "3   0.284297        0           1   0  1562  \n",
       "4   0.339143        0           1   0  1600  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# path to where the data lies\n",
    "#dpath = './BikeShareing/'\n",
    "df = pd.read_csv(\"FE_day.csv\")\n",
    "\n",
    "#通过观察前5行，了解数据每列（特征）的概况\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  数据基本信息\n",
    "样本数目、特征维数\n",
    "每个特征的类型、空值样本的数目、数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 731 entries, 0 to 730\n",
      "Data columns (total 35 columns):\n",
      "instant         731 non-null int64\n",
      "season_1        731 non-null int64\n",
      "season_2        731 non-null int64\n",
      "season_3        731 non-null int64\n",
      "season_4        731 non-null int64\n",
      "mnth_1          731 non-null int64\n",
      "mnth_2          731 non-null int64\n",
      "mnth_3          731 non-null int64\n",
      "mnth_4          731 non-null int64\n",
      "mnth_5          731 non-null int64\n",
      "mnth_6          731 non-null int64\n",
      "mnth_7          731 non-null int64\n",
      "mnth_8          731 non-null int64\n",
      "mnth_9          731 non-null int64\n",
      "mnth_10         731 non-null int64\n",
      "mnth_11         731 non-null int64\n",
      "mnth_12         731 non-null int64\n",
      "weathersit_1    731 non-null int64\n",
      "weathersit_2    731 non-null int64\n",
      "weathersit_3    731 non-null int64\n",
      "weekday_0       731 non-null int64\n",
      "weekday_1       731 non-null int64\n",
      "weekday_2       731 non-null int64\n",
      "weekday_3       731 non-null int64\n",
      "weekday_4       731 non-null int64\n",
      "weekday_5       731 non-null int64\n",
      "weekday_6       731 non-null int64\n",
      "temp            731 non-null float64\n",
      "atemp           731 non-null float64\n",
      "hum             731 non-null float64\n",
      "windspeed       731 non-null float64\n",
      "holiday         731 non-null int64\n",
      "workingday      731 non-null int64\n",
      "yr              731 non-null int64\n",
      "cnt             731 non-null int64\n",
      "dtypes: float64(4), int64(31)\n",
      "memory usage: 200.0 KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  数据准备\n",
    "校验集数据划分,对全体数据进行随机划分，其中80%为训练数据，20%为校验数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从原始数据中分离输入特征x和输出y\n",
    "y = df[\"cnt\"]\n",
    "\n",
    "X = df.drop([\"cnt\"], axis = 1)\n",
    "\n",
    "#特征名称，用于后续显示权重系数对应的特征\n",
    "feat_names = X.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(584, 34)"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将数据分割训练数据与测试数据\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# 随机采样20%的数据构建测试样本，其余作为训练样本\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.2)\n",
    "X_train.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、确定模型类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 尝试缺省参数的线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>coef</th>\n",
       "      <th>columns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>4550.708897</td>\n",
       "      <td>yr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>2654.792827</td>\n",
       "      <td>temp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1287.992360</td>\n",
       "      <td>mnth_9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>995.293778</td>\n",
       "      <td>atemp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>929.887649</td>\n",
       "      <td>mnth_10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>914.410909</td>\n",
       "      <td>weathersit_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>830.579518</td>\n",
       "      <td>season_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>586.296405</td>\n",
       "      <td>mnth_12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>517.803038</td>\n",
       "      <td>mnth_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>409.589079</td>\n",
       "      <td>weathersit_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>407.138803</td>\n",
       "      <td>mnth_11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>238.417312</td>\n",
       "      <td>weekday_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>216.956549</td>\n",
       "      <td>workingday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>189.797216</td>\n",
       "      <td>mnth_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>85.141889</td>\n",
       "      <td>season_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>77.516263</td>\n",
       "      <td>weekday_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>66.464787</td>\n",
       "      <td>weekday_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>43.650546</td>\n",
       "      <td>weekday_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5.009200</td>\n",
       "      <td>mnth_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-6.919060</td>\n",
       "      <td>instant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>-31.943212</td>\n",
       "      <td>weekday_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-130.807162</td>\n",
       "      <td>season_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>-191.612446</td>\n",
       "      <td>weekday_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>-202.493250</td>\n",
       "      <td>weekday_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-203.137582</td>\n",
       "      <td>mnth_7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>-263.761415</td>\n",
       "      <td>holiday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-485.714239</td>\n",
       "      <td>mnth_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-541.439917</td>\n",
       "      <td>mnth_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-784.914245</td>\n",
       "      <td>season_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>-1174.845790</td>\n",
       "      <td>windspeed</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-1207.111892</td>\n",
       "      <td>mnth_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>-1298.592138</td>\n",
       "      <td>hum</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>-1323.999988</td>\n",
       "      <td>weathersit_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1486.521042</td>\n",
       "      <td>mnth_1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           coef       columns\n",
       "33  4550.708897            yr\n",
       "27  2654.792827          temp\n",
       "13  1287.992360        mnth_9\n",
       "28   995.293778         atemp\n",
       "14   929.887649       mnth_10\n",
       "17   914.410909  weathersit_1\n",
       "4    830.579518      season_4\n",
       "16   586.296405       mnth_12\n",
       "12   517.803038        mnth_8\n",
       "18   409.589079  weathersit_2\n",
       "15   407.138803       mnth_11\n",
       "26   238.417312     weekday_6\n",
       "32   216.956549    workingday\n",
       "10   189.797216        mnth_6\n",
       "2     85.141889      season_2\n",
       "25    77.516263     weekday_5\n",
       "23    66.464787     weekday_3\n",
       "24    43.650546     weekday_4\n",
       "9      5.009200        mnth_5\n",
       "0     -6.919060       instant\n",
       "22   -31.943212     weekday_2\n",
       "3   -130.807162      season_3\n",
       "20  -191.612446     weekday_0\n",
       "21  -202.493250     weekday_1\n",
       "11  -203.137582        mnth_7\n",
       "31  -263.761415       holiday\n",
       "8   -485.714239        mnth_4\n",
       "7   -541.439917        mnth_3\n",
       "1   -784.914245      season_1\n",
       "30 -1174.845790     windspeed\n",
       "6  -1207.111892        mnth_2\n",
       "29 -1298.592138           hum\n",
       "19 -1323.999988  weathersit_3\n",
       "5  -1486.521042        mnth_1"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 线性回归\n",
    "#class sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False, copy_X=True, n_jobs=1)\n",
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "# 1.使用默认配置初始化学习器实例\n",
    "lr = LinearRegression()\n",
    "\n",
    "# 2.用训练数据训练模型参数\n",
    "lr.fit(X_train, y_train)\n",
    "\n",
    "# 3. 用训练好的模型对测试集进行预测\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "\n",
    "\n",
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef\":list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.1.1 模型评价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LinearRegression on test is 0.8279474225980329\n",
      "The r2 score of LinearRegression on train is 0.8516480637403496\n"
     ]
    }
   ],
   "source": [
    "# 使用r2_score评价模型在测试集和训练集上的性能，并输出评估结果\n",
    "#测试集\n",
    "print ('The r2 score of LinearRegression on test is', r2_score(y_test, y_test_pred_lr))\n",
    "#训练集\n",
    "print ('The r2 score of LinearRegression on train is', r2_score(y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The rmse of LinearRegression on test is 814.4749076863644\n",
      "The rmse of LinearRegression on train is 742.7543512758713\n"
     ]
    }
   ],
   "source": [
    "# 使用RMSE评价模型在测试集和训练集上的性能，并输出评估结果\n",
    "from sklearn.metrics import mean_squared_error\n",
    "#测试集\n",
    "print ('The rmse of LinearRegression on test is', (mean_squared_error(y_test, y_test_pred_lr)**0.5))\n",
    "#训练集\n",
    "print ('The rmse of LinearRegression on train is', (mean_squared_error(y_train, y_train_pred_lr)**0.5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAekAAAFsCAYAAADos0H8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAH7FJREFUeJzt3X+8VXWd7/HXRzhCIwKiYAg1YKH5G+xocEujKLXBxHncSrumNJL2sJprdWvSvOXp10xdvc3Nm9MMtx9i2ogy4+BMj2b0MjJO3tIBI9PQIJNECY6YP9BMwc/9Y6+DGziHs88vzpezX8/HYz/2Wt+19lrf9d3n7Pf5rvU9a0dmIkmSyrP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aWll5vo6chfTa1G/UDHFzdWJ/M3BARE7p+mSRpd9avX8/+++/PlClTiOivLz/TYMlMNm/ezPr165k6dWqvt9PwwLHqm3zOAG7qyQ4i4sKIWBERK9rb23taP0lqCs8//zwHHnigAT1ERAQHHnhgn8+M9GR09zuAezKz4/tnN0bExKoyE6l9w88uMnNhZrZmZuv48Q39W5gkNSUDemjpj/ezJyH9Xl4+1Q21r5zr+L7X+cDSPtdGkiRt19A16Yj4A+DtwAfrir8M3BgRC4BfA+/u/+pJUnNqa9vz2xs2bBjHHHMMW7duZerUqXz3u99l7NixPd7XBz7wAT7+8Y9z5JFH7lB+zTXXsGLFCr7+9a/3eJsAo0aNYsuWLQ2tO3v2bK688kpaW1u3l61YsYJrr72Wq666qlf7HwwN9aQz87nMPDAzn6or25yZczJzWvX8xMBVU5I00F7xilewatUq7rvvPsaNG8fVV1/dq+1885vf3CWgS9Da2jrgAb1t27Z+3Z53HJMk7WLWrFk8+uij2+evuOIKTjjhBI499lguv/xyAJ599lnmzp3Lcccdx9FHH83ixYuBWi+24xbQ3/nOdzjssMN485vfzJ133rl9e+9///tZsmTJ9vlRo0YBsGXLFubMmcPxxx/PMcccw9Klu15J3bBhAyeffDLTp0/n6KOP5t///d8bOqbly5dz+umnA9DW1sb555/P7NmzOfTQQ3cI7+uuu44TTzyR6dOn88EPfnB78F500UW0trZy1FFHbW8DqN3y+vOf/zxvetObuOmmHo2t7tYevXe3JKl827ZtY9myZSxYsACAW2+9lTVr1nD33XeTmZxxxhnccccdtLe3c8ghh/D9738fgKeeemqH7WzYsIHLL7+clStXMmbMGN7ylrcwY8aM3e575MiR3HzzzYwePZrHH3+cmTNncsYZZ+wwCOt73/sep556Kpdddhnbtm3jueee69VxPvDAA9x+++0888wzHH744Vx00UWsXbuWxYsXc+edd9LS0sKHPvQhrr/+es477zy+9KUvMW7cOLZt28acOXO49957OfbYY7fX+4c//GGv6rE7hrQkCYDf/e53TJ8+nYcffpjXv/71vP3tbwdqIX3rrbduD9gtW7awZs0aTjrpJD7xiU/wqU99itNPP52TTjpph+3dddddzJ49m47/7DnrrLP4xS9+sds6ZCaf/vSnueOOO9hnn3149NFH2bhxI6985Su3r3PCCSdw/vnn8+KLL3LmmWcyffr0Xh3v3LlzGTFiBCNGjGDChAls3LiRZcuWsXLlSk444YTtbTJhQu02IDfeeCMLFy5k69atbNiwgZ///OfbQ/qss87qVR264+luSRLw8jXpdevW8cILL2y/Jp2ZXHrppaxatYpVq1axdu1aFixYwGGHHcbKlSs55phjuPTSS/n85z+/yza7+jek4cOH89JLL23f/gsvvADA9ddfT3t7OytXrmTVqlUcfPDBu/yv8cknn8wdd9zBpEmTOPfcc7n22mt7dbwjRozYPj1s2DC2bt1KZjJ//vztx/rggw/S1tbGr371K6688kqWLVvGvffey9y5c3eo13777derOnTHnrSkTjU6uri/RyFr8I0ZM4arrrqKefPmcdFFF3Hqqafymc98hnPOOYdRo0bx6KOP0tLSwtatWxk3bhzve9/7GDVqFNdcc80O23nDG97AxRdfzObNmxk9ejQ33XQTxx13HFC7jrty5Ure8573sHTpUl588UWgdsp8woQJtLS0cPvtt7Nu3a7f6rhu3TomTZrEBRdcwLPPPss999zDeeed1y/HPmfOHObNm8fHPvYxJkyYwBNPPMEzzzzD008/zX777ceYMWPYuHEjP/jBD5g9e3a/7HN3DGlJKtBg//EzY8YMjjvuOG644QbOPfdcVq9ezaxZs4DaIK/rrruOtWvX8slPfpJ99tmHlpYWvvGNb+ywjYkTJ9LW1sasWbOYOHEixx9//PZBWBdccAHz5s3jxBNPZM6cOdt7oueccw7vfOc7aW1tZfr06bzuda/bpW7Lly/niiuuoKWlhVGjRnXZk547d+72+2bPmjWLD3/4w90e95FHHskXv/hFTjnlFF566SVaWlq4+uqrmTlzJjNmzOCoo47i0EMP5Y1vfGPjjdkHkZl7ZEcAra2t2THiT1LZ7EnvWatXr+aII44Y7Gqon3X2vkbEysxs7eIlO/CatCRJhTKkJUkqlCEtSYXYk5cfNfD64/00pCWpACNHjmTz5s0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qQj4v9GxH2dPOYBZOZlmfkq4Hqg4z7jXd3itKflReiuDap1LgO2UmsHGGJtAI21Q2cv66Rsr26HXhiKx9QXQ/q9j4hRwN8BH93pbOMuq3ZStte3Q2Zuy8zp1M4qnkjtUtguq1XPe7wN+nLv7iJl5tsaXPV7wPeBy+n6Fqfrgdk7lS+vyid3sn4RumuDamDH6cCc6nQ97P42r52VP07tdNfw6i/KotoAevSzUG/ItUMvNOstfzdGxMTM3FCdxt1Ulff082GvEREt1AL6+sz8+6q46doBIDOfjIjl1K5Jd/U73dEG6yNiODCG2mWTAfudGXI96d2JiGl1s2cAD1TTtwDnVaMXZwJPVad5/gU4JSIOqEY4ngL8S7XsmYiYWV2POA9YuueOpPci4jTgU8AZmflc3aJbgLOr0YtTgWnUBsd1evvXKtxvB95VvX4+e0kbdMN2aN5b/t5C7f2DHd/HHn0+7OlK91b12fUtYHVmfrVuUdO0Q0SMj+o/XCLiFcDbqF2b7+p3ur5t3gX8a/UZ0NXnRt/150i50h/U/mK8D7gX+EdgUr48wu9qatcifsaOo33PpzYIYC3wJ3XlrdW2fgl8nerubaU/quN4BFhVPf66btll1fE8SN1odWqjOn9RLbusrvzQ6gdxLXATMGKwj68H7fDH1P76/T2wkdofX03XDrtpn06Pdag8gL8FNgAvVj8HC6hdW1wGrKmex1Xr9vjzYW94AG+idkr23rrPgz9qpnYAjgV+UrXBfcBnq/JOf6eBkdX82mr5oXXb6vRzo68PbwsqSVKhmup0tyRJexNDWpKkQhnSkiQVypCWJKlQhrQkSYUypCVJKpQhLUlSof4/YGdWY/RHppMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fd335f8ff60>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#在训练集上观察预测残差的分布，看是否符合模型假设：噪声为0均值的高斯噪声\n",
    "f, ax = plt.subplots(figsize=(7, 5)) \n",
    "f.tight_layout() \n",
    "ax.hist(y_train - y_train_pred_lr, bins=40, label='Residuals Linear', color='b', alpha=.5); \n",
    "ax.set_title(\"Histogram of Residuals\") \n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "残差分布和高斯分布比较匹配。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAARgAAADQCAYAAADcQn7hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJztnX2UVGeZ4H9PdxdQ5KtB0SENCImZZMnEhMgaIrNzTNSQqCHoJAaNO+hmzPo1huhEYcadgB8rGk2ie2bUrFlPVDTkazoQVGRM3D1HByLYJEgSJpgYQhMNCo0KbeiPZ/+4721uV99761ZXvVW3up/fOXW67ns/6qkL96n3fT5FVTEMw/BBS6MFMAxj7GIKxjAMb5iCMQzDG6ZgDMPwhikYwzC8YQrGMAxvmIIxDMMbpmAMw/CGKRjDMLzR1mgBfPDSl75UZ8+e3WgxDGPMsn379t+q6rRyx41JBTN79my2bdvWaDEMY8wiIs9mOc7rEklEbhCRXSLyCxH5rohMEpE5IrJVRJ4SkXUiMsEdO9Ft73H7Z0eus9KN7xaRRT5lNgyjdnhTMCLSAXwYmK+qfwG0AkuBzwG3quoZwCHgWnfKtcAhVX0lcKs7DhGZ6847G7gU+BcRafUlt2EYtcO3kbcNKIpIGzAZeB64GLjX7b8TWOLeX+G2cftfLyLixu9S1RdV9RlgD/Aaz3IbRi7p7Opm4ZqHmLNiIwvXPERnV3ejRUrFm4JR1W7gC8BeAsVyGNgO9KhqvztsH9Dh3ncAz7lz+93xL4mOx5wzhIhcJyLbRGTbgQMHav+FDKPBdHZ1s/L+nXT39KJAd08vK+/fmWsl43OJNIVg9jEHOBU4Abgs5tCwII0k7EsaHz6geruqzlfV+dOmlTVuG+OIZvvVT+LmTbvp7RsYNtbbN8DNm3Y3SKLy+PQivQ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YxiGN0zBGIbhDVMwhmF4wxSMYRjeMAVjGIY3TMEYhuGN/w/XDGHvspD0tAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fd32c444710>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(4, 3))\n",
    "plt.scatter(y_train, y_train_pred_lr)\n",
    "plt.plot([-3, 3], [-3, 3], '--k')   #数据已经标准化，3倍标准差即可\n",
    "plt.axis('tight')\n",
    "plt.xlabel('True cnt')\n",
    "plt.ylabel('Predicted cnt')\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在y的真值小于6000的时候，个别数据预测值偏高，总体来数比较符合真实数据。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 正则化的线性回归（L2正则 --> 岭回归）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of RidgeCV on test is 0.828974067633319\n",
      "The r2 score of RidgeCV on train is 0.8497700029725634\n"
     ]
    }
   ],
   "source": [
    "#岭回归／L2正则\n",
    "#class sklearn.linear_model.RidgeCV(alphas=(0.1, 1.0, 10.0), fit_intercept=True, \n",
    "#                                  normalize=False, scoring=None, cv=None, gcv_mode=None, \n",
    "#                                  store_cv_values=False)\n",
    "from sklearn.linear_model import  RidgeCV\n",
    "\n",
    "#1. 设置超参数（正则参数）范围\n",
    "alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "#n_alphas = 20\n",
    "#alphas = np.logspace(-5,2,n_alphas)\n",
    "\n",
    "#2. 生成一个RidgeCV实例\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)  \n",
    "\n",
    "#3. 模型训练\n",
    "ridge.fit(X_train, y_train)    \n",
    "\n",
    "#4. 预测\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)\n",
    "\n",
    "\n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print ('The r2 score of RidgeCV on test is', r2_score(y_test, y_test_pred_ridge))\n",
    "print ('The r2 score of RidgeCV on train is', r2_score(y_train, y_train_pred_ridge))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练数据结果略高于测试数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The rmse of LinearRegression on test is 812.041268667859\n",
      "The rmse of LinearRegression on train is 747.441013159984\n"
     ]
    }
   ],
   "source": [
    "# 使用RMSE评价模型在测试集和训练集上的性能，并输出评估结果\n",
    "from sklearn.metrics import mean_squared_error\n",
    "#测试集\n",
    "print ('The rmse of LinearRegression on test is', (mean_squared_error(y_test, y_test_pred_ridge)**0.5))\n",
    "#训练集\n",
    "print ('The rmse of LinearRegression on train is', (mean_squared_error(y_train, y_train_pred_ridge)**0.5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从结果得知模型可靠。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAEKCAYAAAC7c+rvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XtwVOeZ5/Hvoyt3ECDEHeGAccCxHSzM1XYc24C9SYidOLGTCRg84002U5tkanbHu1MV1yZbNZOd2Zkq78xkNjMWhk2G2ImdsZPYAUwcJ+JmBMHmaoMtAQJ0AXG/SEj97B995DRCarWEuk+r+/epUvXpt9/T79NHoJ/69KvzmrsjIiIShpywCxARkeylEBIRkdAohEREJDQKIRERC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6S762AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fd32c562940>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 1.0\n"
     ]
    }
   ],
   "source": [
    "mse_mean = np.mean(ridge.cv_values_, axis = 0)\n",
    "plt.plot(np.log10(alphas), mse_mean.reshape(len(alphas),1)) \n",
    "\n",
    "#这是为了标出最佳参数的位置，不是必须\n",
    "#plt.plot(np.log10(ridge.alpha_)*np.ones(3), [0.28, 0.29, 0.30])\n",
    "\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()\n",
    "\n",
    "print ('alpha is:', ridge.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>columns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>4550.708897</td>\n",
       "      <td>1504.623924</td>\n",
       "      <td>yr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>2654.792827</td>\n",
       "      <td>1778.493414</td>\n",
       "      <td>temp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1287.992360</td>\n",
       "      <td>678.352931</td>\n",
       "      <td>mnth_9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>995.293778</td>\n",
       "      <td>1546.374034</td>\n",
       "      <td>atemp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>929.887649</td>\n",
       "      <td>84.873020</td>\n",
       "      <td>mnth_10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>914.410909</td>\n",
       "      <td>914.843092</td>\n",
       "      <td>weathersit_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>830.579518</td>\n",
       "      <td>767.205317</td>\n",
       "      <td>season_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>586.296405</td>\n",
       "      <td>-824.928596</td>\n",
       "      <td>mnth_12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>517.803038</td>\n",
       "      <td>205.686009</td>\n",
       "      <td>mnth_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>409.589079</td>\n",
       "      <td>388.865215</td>\n",
       "      <td>weathersit_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>407.138803</td>\n",
       "      <td>-725.828529</td>\n",
       "      <td>mnth_11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>238.417312</td>\n",
       "      <td>227.515740</td>\n",
       "      <td>weekday_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>216.956549</td>\n",
       "      <td>212.558710</td>\n",
       "      <td>workingday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>189.797216</td>\n",
       "      <td>369.663154</td>\n",
       "      <td>mnth_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>85.141889</td>\n",
       "      <td>110.998614</td>\n",
       "      <td>season_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>77.516263</td>\n",
       "      <td>81.396550</td>\n",
       "      <td>weekday_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>66.464787</td>\n",
       "      <td>59.857933</td>\n",
       "      <td>weekday_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>43.650546</td>\n",
       "      <td>62.795850</td>\n",
       "      <td>weekday_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5.009200</td>\n",
       "      <td>387.713628</td>\n",
       "      <td>mnth_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-6.919060</td>\n",
       "      <td>1.417157</td>\n",
       "      <td>instant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>-31.943212</td>\n",
       "      <td>-34.140080</td>\n",
       "      <td>weekday_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-130.807162</td>\n",
       "      <td>-91.388275</td>\n",
       "      <td>season_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>-191.612446</td>\n",
       "      <td>-191.851510</td>\n",
       "      <td>weekday_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>-202.493250</td>\n",
       "      <td>-205.574484</td>\n",
       "      <td>weekday_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-203.137582</td>\n",
       "      <td>-242.548582</td>\n",
       "      <td>mnth_7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>-263.761415</td>\n",
       "      <td>-248.222941</td>\n",
       "      <td>holiday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-485.714239</td>\n",
       "      <td>106.306513</td>\n",
       "      <td>mnth_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-541.439917</td>\n",
       "      <td>298.550050</td>\n",
       "      <td>mnth_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-784.914245</td>\n",
       "      <td>-786.815656</td>\n",
       "      <td>season_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>-1174.845790</td>\n",
       "      <td>-1087.773198</td>\n",
       "      <td>windspeed</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-1207.111892</td>\n",
       "      <td>-143.125249</td>\n",
       "      <td>mnth_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>-1298.592138</td>\n",
       "      <td>-1142.397276</td>\n",
       "      <td>hum</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>-1323.999988</td>\n",
       "      <td>-1303.708307</td>\n",
       "      <td>weathersit_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1486.521042</td>\n",
       "      <td>-194.714350</td>\n",
       "      <td>mnth_1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        coef_lr   coef_ridge       columns\n",
       "33  4550.708897  1504.623924            yr\n",
       "27  2654.792827  1778.493414          temp\n",
       "13  1287.992360   678.352931        mnth_9\n",
       "28   995.293778  1546.374034         atemp\n",
       "14   929.887649    84.873020       mnth_10\n",
       "17   914.410909   914.843092  weathersit_1\n",
       "4    830.579518   767.205317      season_4\n",
       "16   586.296405  -824.928596       mnth_12\n",
       "12   517.803038   205.686009        mnth_8\n",
       "18   409.589079   388.865215  weathersit_2\n",
       "15   407.138803  -725.828529       mnth_11\n",
       "26   238.417312   227.515740     weekday_6\n",
       "32   216.956549   212.558710    workingday\n",
       "10   189.797216   369.663154        mnth_6\n",
       "2     85.141889   110.998614      season_2\n",
       "25    77.516263    81.396550     weekday_5\n",
       "23    66.464787    59.857933     weekday_3\n",
       "24    43.650546    62.795850     weekday_4\n",
       "9      5.009200   387.713628        mnth_5\n",
       "0     -6.919060     1.417157       instant\n",
       "22   -31.943212   -34.140080     weekday_2\n",
       "3   -130.807162   -91.388275      season_3\n",
       "20  -191.612446  -191.851510     weekday_0\n",
       "21  -202.493250  -205.574484     weekday_1\n",
       "11  -203.137582  -242.548582        mnth_7\n",
       "31  -263.761415  -248.222941       holiday\n",
       "8   -485.714239   106.306513        mnth_4\n",
       "7   -541.439917   298.550050        mnth_3\n",
       "1   -784.914245  -786.815656      season_1\n",
       "30 -1174.845790 -1087.773198     windspeed\n",
       "6  -1207.111892  -143.125249        mnth_2\n",
       "29 -1298.592138 -1142.397276           hum\n",
       "19 -1323.999988 -1303.708307  weathersit_3\n",
       "5  -1486.521042  -194.714350        mnth_1"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 超参数调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "#from sklearn.model_selection import GridSearchCV\n",
    "#from sklearn.linear_model import linear_model\n",
    "#ridge = Ridge()\n",
    "#ridge_model = GridSearchCV(\n",
    "#ridge,param_grid=({'alpha':np.linspace(0,5,10)}),cv = 10)\n",
    "#ridge_model.fit(x_train, y_train)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 正则化的线性回归（L1正则 --> Lasso）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LassoCV on test is 0.8283949861658035\n",
      "The r2 score of LassoCV on train is 0.8514742621740345\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/model_selection/_split.py:2053: FutureWarning: You should specify a value for 'cv' instead of relying on the default value. The default value will change from 3 to 5 in version 0.22.\n",
      "  warnings.warn(CV_WARNING, FutureWarning)\n",
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:492: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:492: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:492: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:492: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:492: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:492: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:492: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:492: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n",
      "/home/surveyzhang/.local/lib/python3.6/site-packages/sklearn/linear_model/coordinate_descent.py:492: ConvergenceWarning: Objective did not converge. You might want to increase the number of iterations. Fitting data with very small alpha may cause precision problems.\n",
      "  ConvergenceWarning)\n"
     ]
    }
   ],
   "source": [
    "#### Lasso／L1正则\n",
    "# class sklearn.linear_model.LassoCV(eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, \n",
    "#                                    normalize=False, precompute=’auto’, max_iter=1000, \n",
    "#                                    tol=0.0001, copy_X=True, cv=None, verbose=False, n_jobs=1,\n",
    "#                                    positive=False, random_state=None, selection=’cyclic’)\n",
    "from sklearn.linear_model import LassoCV\n",
    "\n",
    "#1. 设置超参数搜索范围\n",
    "alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "\n",
    "#2. 生成学习器实例\n",
    "lasso = LassoCV(alphas=alphas)\n",
    "\n",
    "#1. 设置超参数搜索范围\n",
    "#Lasso可以自动确定最大的alpha，所以另一种设置alpha的方式是设置最小的alpha值（eps） 和 超参数的数目（n_alphas），\n",
    "#然后LassoCV对最小值和最大值之间在log域上均匀取值n_alphas个\n",
    "# np.logspace(np.log10(alpha_max * eps), np.log10(alpha_max),num=n_alphas)[::-1]\n",
    "\n",
    "#2 生成LassoCV实例（默认超参数搜索范围）\n",
    "#lasso = LassoCV()  \n",
    "\n",
    "#3. 训练（内含CV）\n",
    "lasso.fit(X_train, y_train)  \n",
    "\n",
    "#4. 测试\n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    "\n",
    "\n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print ('The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso))\n",
    "print ('The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The rmse of LinearRegression on test is 813.4148635007384\n",
      "The rmse of LinearRegression on train is 743.1893104872605\n"
     ]
    }
   ],
   "source": [
    "# 使用RMSE评价模型在测试集和训练集上的性能，并输出评估结果\n",
    "from sklearn.metrics import mean_squared_error\n",
    "#测试集\n",
    "print ('The rmse of LinearRegression on test is', (mean_squared_error(y_test, y_test_pred_lasso)**0.5))\n",
    "#训练集\n",
    "print ('The rmse of LinearRegression on train is', (mean_squared_error(y_train, y_train_pred_lasso)**0.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAEKCAYAAAC7c+rvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XmUldWZ7/HvUyPzWMUgMxFFxQlLwJgYOyQKdiKadgZCp+14k5vcm+5evVpduatdN+kh6fRtV+xOcq/dZkWBgIZoxERjcIrpDgUUKArBoQRPVTEWFGMx1PTcP84uPBRVpwbqnPfUOb/PWrXOe/a733c/5wXOU+9+N3ubuyMiIhKFvKgDEBGR3KUkJCIikVESEhGRyCgJiYhIZJSEREQkMkpCIiISGSUhERGJjJKQiIhERklIREQiUxB1AJmupKTEJ0+eHHUYIiJ9ysaNG/e7e2ln9ZSEOjF58mQqKiqiDkNEpE8xs1hX6qk7TkREIqMkJCIikVESEhGR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vNbNfd7W+yLlQEhLJQGHesHx3357utt29FthtZtemu23JPUpCIikW/pf598xsi8XXa7ozlOeFqVm2mtkvzex5M7stHLaQhFkazOxHYaLUrWb2vzto55iZ/R8z22RmL5tZacLu281svZm9Z2afDPUnm9nvQv1NZvbxhPq/CDGIpJSSkEjqfQG4Argc+AzwvTClzReAycClwJ8D1yQccy2wMeH9N929DLgM+JSZXdZOOwOBTe4+E/gt8FDCvgJ3nwX8RUL5PuCzof6dwCMJ9SuAT3b/o4p0jyYwFUm9TxCfTboZ2GtmvwWuDuU/c/cWYI+ZvZpwzFigNuH9HWF5jYKw72Li06wkagGeDNvLgMSJKFu3NxJPfACFwL+Z2RVAM3BBQv19xKdPEkkpJSGR1OtogblkC8+dID4nHGY2Bfhr4Gp3P2hmP2nd14nEOblOhddmPvp3/5fE5+y7nHivyMmE+v1CDCIppe44kdR7HbjTzPLDc5rriE80+p/EF33LM7PRxJd3brUNOD9sDwHqgcOh3vwO2skjPos2wD3h/MkMBXaHO7HFxJfsbnUBmTGLumQ53QmJpN4zxJ/3bCZ+d/I37r7HzH4OzCX+Zf8e8dUsD4djfkU8Kb3k7pvN7A3iMyxvB/6rg3bqgUvMbGM4z52dxPVD4OdmdjvxWa7rE/b9UYhBJKU0i7ZIhMxskMdX0hxJ/O7o2pCg+hNPDNeGZ0ldOdcxdx/US3G9Dixw94O9cT6RjuhOSCRavzSzYUAR8G133wPg7ifM7CFgHFCVzoBCl+G/KAFJOuhOSEREIqOBCSIiEhklIRERiYySkIiIREZJSEREIqMkJCIikVESEhGRyPx/uTrPbmHlWEgAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fd32c56e9b0>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 0.1\n"
     ]
    }
   ],
   "source": [
    "mses = np.mean(lasso.mse_path_, axis = 1)\n",
    "plt.plot(np.log10(lasso.alphas_), mses) \n",
    "#plt.plot(np.log10(lasso.alphas_)*np.ones(3), [0.3, 0.4, 1.0])\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()    \n",
    "            \n",
    "print ('alpha is:', lasso.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>coef_lasso</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>columns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>3472.533627</td>\n",
       "      <td>4550.708897</td>\n",
       "      <td>1504.623924</td>\n",
       "      <td>yr</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>2609.974800</td>\n",
       "      <td>2654.792827</td>\n",
       "      <td>1778.493414</td>\n",
       "      <td>temp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>924.516638</td>\n",
       "      <td>1287.992360</td>\n",
       "      <td>678.352931</td>\n",
       "      <td>mnth_9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>1031.600940</td>\n",
       "      <td>995.293778</td>\n",
       "      <td>1546.374034</td>\n",
       "      <td>atemp</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>472.969983</td>\n",
       "      <td>929.887649</td>\n",
       "      <td>84.873020</td>\n",
       "      <td>mnth_10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>499.554848</td>\n",
       "      <td>914.410909</td>\n",
       "      <td>914.843092</td>\n",
       "      <td>weathersit_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>759.982846</td>\n",
       "      <td>830.579518</td>\n",
       "      <td>767.205317</td>\n",
       "      <td>season_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>-45.208703</td>\n",
       "      <td>586.296405</td>\n",
       "      <td>-824.928596</td>\n",
       "      <td>mnth_12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>244.551040</td>\n",
       "      <td>517.803038</td>\n",
       "      <td>205.686009</td>\n",
       "      <td>mnth_8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>409.589079</td>\n",
       "      <td>388.865215</td>\n",
       "      <td>weathersit_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>-141.664562</td>\n",
       "      <td>407.138803</td>\n",
       "      <td>-725.828529</td>\n",
       "      <td>mnth_11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>0.975214</td>\n",
       "      <td>238.417312</td>\n",
       "      <td>227.515740</td>\n",
       "      <td>weekday_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>26.039729</td>\n",
       "      <td>216.956549</td>\n",
       "      <td>212.558710</td>\n",
       "      <td>workingday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>99.293831</td>\n",
       "      <td>189.797216</td>\n",
       "      <td>369.663154</td>\n",
       "      <td>mnth_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5.856737</td>\n",
       "      <td>85.141889</td>\n",
       "      <td>110.998614</td>\n",
       "      <td>season_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>28.802096</td>\n",
       "      <td>77.516263</td>\n",
       "      <td>81.396550</td>\n",
       "      <td>weekday_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>17.401523</td>\n",
       "      <td>66.464787</td>\n",
       "      <td>59.857933</td>\n",
       "      <td>weekday_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>-0.000000</td>\n",
       "      <td>43.650546</td>\n",
       "      <td>62.795850</td>\n",
       "      <td>weekday_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>8.049479</td>\n",
       "      <td>5.009200</td>\n",
       "      <td>387.713628</td>\n",
       "      <td>mnth_5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-3.972530</td>\n",
       "      <td>-6.919060</td>\n",
       "      <td>1.417157</td>\n",
       "      <td>instant</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>-79.989343</td>\n",
       "      <td>-31.943212</td>\n",
       "      <td>-34.140080</td>\n",
       "      <td>weekday_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-199.683696</td>\n",
       "      <td>-130.807162</td>\n",
       "      <td>-91.388275</td>\n",
       "      <td>season_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>-427.681280</td>\n",
       "      <td>-191.612446</td>\n",
       "      <td>-191.851510</td>\n",
       "      <td>weekday_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>-249.463024</td>\n",
       "      <td>-202.493250</td>\n",
       "      <td>-205.574484</td>\n",
       "      <td>weekday_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-386.248257</td>\n",
       "      <td>-203.137582</td>\n",
       "      <td>-242.548582</td>\n",
       "      <td>mnth_7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>-447.146754</td>\n",
       "      <td>-263.761415</td>\n",
       "      <td>-248.222941</td>\n",
       "      <td>holiday</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-392.887064</td>\n",
       "      <td>-485.714239</td>\n",
       "      <td>106.306513</td>\n",
       "      <td>mnth_4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-358.415367</td>\n",
       "      <td>-541.439917</td>\n",
       "      <td>298.550050</td>\n",
       "      <td>mnth_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-864.040202</td>\n",
       "      <td>-784.914245</td>\n",
       "      <td>-786.815656</td>\n",
       "      <td>season_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>-1180.476349</td>\n",
       "      <td>-1174.845790</td>\n",
       "      <td>-1087.773198</td>\n",
       "      <td>windspeed</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-938.564141</td>\n",
       "      <td>-1207.111892</td>\n",
       "      <td>-143.125249</td>\n",
       "      <td>mnth_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>-1317.015682</td>\n",
       "      <td>-1298.592138</td>\n",
       "      <td>-1142.397276</td>\n",
       "      <td>hum</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>-1730.522971</td>\n",
       "      <td>-1323.999988</td>\n",
       "      <td>-1303.708307</td>\n",
       "      <td>weathersit_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1130.689866</td>\n",
       "      <td>-1486.521042</td>\n",
       "      <td>-194.714350</td>\n",
       "      <td>mnth_1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     coef_lasso      coef_lr   coef_ridge       columns\n",
       "33  3472.533627  4550.708897  1504.623924            yr\n",
       "27  2609.974800  2654.792827  1778.493414          temp\n",
       "13   924.516638  1287.992360   678.352931        mnth_9\n",
       "28  1031.600940   995.293778  1546.374034         atemp\n",
       "14   472.969983   929.887649    84.873020       mnth_10\n",
       "17   499.554848   914.410909   914.843092  weathersit_1\n",
       "4    759.982846   830.579518   767.205317      season_4\n",
       "16   -45.208703   586.296405  -824.928596       mnth_12\n",
       "12   244.551040   517.803038   205.686009        mnth_8\n",
       "18     0.000000   409.589079   388.865215  weathersit_2\n",
       "15  -141.664562   407.138803  -725.828529       mnth_11\n",
       "26     0.975214   238.417312   227.515740     weekday_6\n",
       "32    26.039729   216.956549   212.558710    workingday\n",
       "10    99.293831   189.797216   369.663154        mnth_6\n",
       "2      5.856737    85.141889   110.998614      season_2\n",
       "25    28.802096    77.516263    81.396550     weekday_5\n",
       "23    17.401523    66.464787    59.857933     weekday_3\n",
       "24    -0.000000    43.650546    62.795850     weekday_4\n",
       "9      8.049479     5.009200   387.713628        mnth_5\n",
       "0     -3.972530    -6.919060     1.417157       instant\n",
       "22   -79.989343   -31.943212   -34.140080     weekday_2\n",
       "3   -199.683696  -130.807162   -91.388275      season_3\n",
       "20  -427.681280  -191.612446  -191.851510     weekday_0\n",
       "21  -249.463024  -202.493250  -205.574484     weekday_1\n",
       "11  -386.248257  -203.137582  -242.548582        mnth_7\n",
       "31  -447.146754  -263.761415  -248.222941       holiday\n",
       "8   -392.887064  -485.714239   106.306513        mnth_4\n",
       "7   -358.415367  -541.439917   298.550050        mnth_3\n",
       "1   -864.040202  -784.914245  -786.815656      season_1\n",
       "30 -1180.476349 -1174.845790 -1087.773198     windspeed\n",
       "6   -938.564141 -1207.111892  -143.125249        mnth_2\n",
       "29 -1317.015682 -1298.592138 -1142.397276           hum\n",
       "19 -1730.522971 -1323.999988 -1303.708307  weathersit_3\n",
       "5  -1130.689866 -1486.521042  -194.714350        mnth_1"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T)), \"coef_lasso\":list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过以上数据进行对比，结合各自特征的系数，以及性能上的得分状况，岭回归下的性能得分最高。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
